﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web
{
    public partial class usermain : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["username"] = "tom";
            if (null != Session["username"])
            {
                Label_State.Text = "已登录";
                Label_User.Text = Session["username"].ToString();
                DAL.UserInfoDAL userInfoDAL = (DAL.UserInfoDAL)DALFAC.DataAccess.CreateUserInfo();
                List<Model.Album> albumlist = new List<Model.Album>();
                DAL.AlbumDAL AlbumDal = (DAL.AlbumDAL)DALFAC.DataAccess.CreateAlbum();
                

                if (null != userInfoDAL && null != AlbumDal)
                {
                    Model.UserInfo userInfoModel = userInfoDAL.userNameUserInfo(Session["username"].ToString());
                    int userId = userInfoModel.getUI_userId();
                    Session["userId"] = userId;
                    albumlist = AlbumDal.GetList(userId);

                    Repeater1.DataSource = albumlist;
                    Repeater1.DataBind(); 
                }
                    
            }
            else {
                Response.Redirect("Login.aspx");
            }
        }

        protected void Search_Click(object sender, EventArgs e)
        {
            string userKey = TextBox_Search.Text;
            string url = "albumResult.aspx?key=" + TextBox_Search.Text;
            Response.Redirect(url);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("Index.aspx");
        }

        protected void Create_Click(object sender, EventArgs e)
        {
            string username;
            int userId;
            if (null != Session["username"])
            {
                username = Session["username"].ToString();
            }
            else
            {
                username = "tom";
            }
            DAL.UserInfoDAL userDal = (DAL.UserInfoDAL)DALFAC.DataAccess.CreateUserInfo();
            Model.UserInfo userModel = userDal.userNameUserInfo(username);
            userId = userModel.getUI_userId();


            BLL.AlbumBLL albumBll = (BLL.AlbumBLL)BLLFAC.DataAccess.CreateAlbum();
            if (null != albumBll)
            {
                Model.Album album = new Model.Album();
                album.setAB_userId(userId);
                album.setAB_albumName(TextBox1.Text);
                album.setAB_authority(1);
                album.setAB_description(TextBox_Imp.Text);
                album.setAB_generateTime(DateTime.Now);
                album.setAB_photoCount(0);

                if (albumBll.createAlbum(album))
                {
                    TextBox1.Text = "相册名";
                    TextBox_Imp.Text = "印象";
                    Response.Redirect("usermain.aspx");
                }
                else
                {
                }
            }


        }
    }
}